package cn.view;

import cn.comon.Common;
import cn.entity.Employee;

import javax.swing.*;
import java.awt.*;

public class AdminView extends JFrame {

    Employee emp;
    JDesktopPane desk;

    public AdminView(Employee emp) {
        this.emp = emp;

        // Create a custom menu bar
        CustomMenuBar menubar = new CustomMenuBar();
        this.setJMenuBar(menubar);

        // Create menus
        JMenu menu1 = new JMenu("基本信息管理");
        JMenuItem item1 = new JMenuItem("员工管理");
        item1.addActionListener(e -> empManage());
        JMenuItem item2 = new JMenuItem("客户管理");
        item2.addActionListener(e -> clientManage());
        JMenuItem item3 = new JMenuItem("商品管理");
        item3.addActionListener(e -> goodsManage());
        menu1.add(item1);
        menu1.add(item2);
        menu1.add(item3);

        JMenu menu2 = new JMenu("采购信息管理");
        JMenuItem itempayM = new JMenuItem("采购主表管理");
        itempayM.addActionListener(e -> paymainManage());
        JMenuItem itempayD = new JMenuItem("采购明细管理");
        itempayD.addActionListener(e -> paydetailManage());
        menu2.add(itempayM);
        menu2.add(itempayD);

        JMenu menu3 = new JMenu("统计查询");
        JMenuItem itemEmp = new JMenuItem("员工信息统计");
        itemEmp.addActionListener(e -> EmployeeSQView());
        JMenuItem itemGoods = new JMenuItem("商品信息统计");
        itemGoods.addActionListener(e -> GoodsSQView());
        JMenuItem itemPay = new JMenuItem("采购信息统计");
        itemPay.addActionListener(e -> PaySQView());
        menu3.add(itemEmp);
        menu3.add(itemGoods);
        menu3.add(itemPay);

        // Add menus to the custom menu bar
        menubar.add(menu1);
        menubar.add(menu2);
        menubar.add(menu3);

        // Set desktop pane
        desk = Common.setDesk("picture/2.png");
        getContentPane().add(desk);
        this.setTitle("管理员登录");
        this.setSize(815, 562);
        this.setLocationRelativeTo(null);
        this.setResizable(true);
        this.setVisible(true);
    }

    private void clientManage() {
        desk.add(new ClientManageView());
    }

    protected void empManage() {
        desk.add(new EmployeeManageView());
    }

    protected void goodsManage() {
        desk.add(new GoodsManageView());
    }

    protected void paymainManage() {
        desk.add(new PayMainManageView());
    }

    protected void paydetailManage() {
        desk.add(new PayDetailManageView());
    }

    protected void EmployeeSQView() {
        desk.add(new EmployeeSQView());
    }

    protected void GoodsSQView() {
        desk.add(new GoodsSQView());
    }

    protected void PaySQView() {
        PaySQView paySQView = new PaySQView(); // 创建 PaySQView 实例
        paySQView.setVisible(true); // 设置内部窗体可见
        desk.add(paySQView); // 将内部窗体添加到 JDesktopPane 中
    }

    class CustomMenuBar extends JMenuBar {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            int totalWidth = 0;
            for (int i = 0; i < getMenuCount(); i++) {
                totalWidth += getMenu(i).getPreferredSize().width;
            }

            int startX = (getWidth() - totalWidth) / 2;


            for (int i = 0; i < getMenuCount(); i++) {
                JMenu menu = getMenu(i);
                menu.setLocation(startX, menu.getY());
                startX += menu.getPreferredSize().width;
            }
        }

        @Override
        public Dimension getPreferredSize() {
            Dimension size = super.getPreferredSize();
            size.width = getParent().getWidth();
            return size;
        }
    }

}
